<?php
define('DS', DIRECTORY_SEPARATOR);
define('PS', PATH_SEPARATOR);
define('BP', dirname(dirname(__FILE__)));

/**
 * Error reporting
 */
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);

/**
 * Set include path
 */
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'controllers';
$paths[] = BP . DS . 'app' . DS . 'code';
$paths[] = BP . DS . 'lib';

$app_path = implode(PS, $paths);

set_include_path($app_path . PS . get_include_path());

include_once "etc/config.php";

include_once "Forum/Bootstrap.php";

final class Forum
{
    /**
     * Registry collection
     *
     * @var array
     */
    static private $_registry = array();
    
    /**
     * Frontend main entry point
     * 
     * @return unknown_type
     */
    public static function run()
    {
        global $config;
        try {
            Forum_Bootstrap::run($config);
            Forum_Controller_Front::getInstance()->dispatch();
        } catch (Exception $e) {
            self::printException($e);
        }
    }
    
    /**
     * Register a new variable
     *
     * @param string $key
     * @param mixed $value
     * @param bool $graceful
     */
    public static function register($key, $value, $graceful = false)
    {
        if(isset(self::$_registry[$key])) {
            if ($graceful) {
                return;
            }
            throw new Exception('Forum registry key "'.$key.'" already exists');
        }
        self::$_registry[$key] = $value;
    }
    
    /**
     * Remove variable from registry
     * 
     * @param string $key
     * @return void
     */
    public static function unregister($key)
    {
        if (isset(self::$_registry[$key])) {
            if (is_object(self::$_registry[$key]) && (method_exists(self::$_registry[$key],'__destruct'))) {
                self::$_registry[$key]->__destruct();
            }
            unset(self::$_registry[$key]);
        }
    }

    /**
     * Retrieve a value from registry by a key
     *
     * @param string $key
     * @return mixed
     */
    public static function registry($key)
    {
        if (isset(self::$_registry[$key])) {
            return self::$_registry[$key];
        }
        return null;
    }
   
    /**
     * Retrieve config object from registry
     * 
     * @return Forum_Config Object
     */
    public static function getConfig()
    {
        return Forum::registry('config');
    }
    
    /**
     * Retrieve model object singleton
     *
     * @param $model
     * @param $args
     * @return object
     */
    public static function getSingleton($model, $args = array())
    {
        $parts = explode('/', $model);
        
        $model = 'Model_' . ucfirst($parts[0]);
        
        if (isset($parts[1])) {
            $model .= '_' . ucfirst($parts[1]);
        }
        
        if (null === self::registry($model)) {
            self::register($model, new $model($args));
        }
        return self::registry($model);
    }
    
    /**
     * Display exception
     *
     * @param Exception $e
     */
    public static function printException(Exception $e)
    {
        print '<pre>';
        print $e->getMessage() . "\n\n";
        print $e->getTraceAsString();
        print '</pre>';

        die();
    }
    
}